package com.fplugin.uverify;

import android.app.ProgressDialog;
import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;

import com.alibaba.fastjson.JSONObject;

import java.lang.reflect.Field;
import java.util.Map;
import java.util.UUID;

import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel.Result;
import io.flutter.plugin.common.MethodChannel;

/** UverifyUtil */
public class UverifyUtil {
  private String TAG = "UVerifyUtil=>TAG";

  MethodChannel methodChannel;
  private static Context context;
  private ProgressDialog progressDialog;

  UverifyUtil(Context context, MethodChannel methodChannel){
    this.methodChannel = methodChannel;
    this.context = context;
  }

  /**
   * @function runMainThread 主线程再返回数据
   * @param map {Object}
   * @param result {Result}
   * @param method {String} 返回的监听方法
   */
  public void runMainThread(final Object map, final Result result, final String method) {
    android.os.Handler handler = new Handler(Looper.getMainLooper());
    handler.post(new Runnable() {
      @Override
      public void run() {
        if (result == null && method != null) {
          methodChannel.invokeMethod(method, map);
        } else {
          assert result != null;
          result.success(map);
        }
      }
    });
  }

  /**
   * @function showLoadingDialog 显示加载Dialog
   * @param msg {String}
   */
  public void showLoadingDialog(String msg) {
    if (progressDialog == null) {
      progressDialog = new ProgressDialog(context);
      progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
    }
    progressDialog.setMessage(msg);
    progressDialog.setCancelable(true);
    progressDialog.show();
  }

  /**
   * @function hideLoadingDialog 隐藏加载Dialog
   */
  public void hideLoadingDialog() {
    if (progressDialog != null) {
      progressDialog.dismiss();
    }
  }

  /**
   * @funtion retJSONObject JsonObjdect返回结果
   * @param code 状态码
   * @param msg 状态信息
   * @param data 数据
   * @return 返回内容
   */
  public JSONObject retJSONObject(Object code, String msg, Object data) {
    JSONObject jsonObject = new JSONObject();
    jsonObject.put("code", code);
    jsonObject.put("msg", msg);
    jsonObject.put("data", data);
    return jsonObject;
  }

  /**
   * @function getValueByMapKey 通过对象获取对象值
   * @param Obj Map
   * @param key String
   * @return 返回Object
   */
  public Object getValueByMapKey(Map Obj, String key) {
    if (Obj != null && Obj.containsKey(key)) {
      return Obj.get(key);
    } else {
      return null;
    }
  }

  public String[] splitStringBySeparator(String str, String separator) {
    String regex = ",";
    if(separator != null) {
      regex = separator;
    }
    String[] list = str.split(regex);
    return list;
  }

  /**
   * @function getValueByMethodCallArgument 获取Call参数值
   * @param call {MethodCall}
   * @param key {String}
   * @return Object
   */
  public <T> T getValueByMethodCallArgument(MethodCall call, String key) {
    if (call != null && call.hasArgument(key)) {
      return call.argument(key);
    } else {
      return null;
    }
  }

  /**
   * @function 像素转化成 pix
   * @param {Context} context
   * @param {dp} dp
   * @return int
   */
  public int dp2Pix(Context context, float dp) {
    try {
      float density = context.getResources().getDisplayMetrics().density;
      return (int) (dp * density + 0.5F);
    } catch (Exception e) {
      return (int) dp;
    }
  }


  // 获得全球唯一性的id
  public String getUUID(){
    String id = UUID.randomUUID().toString();
    id = id.replace("-", "");//替换掉中间的那个斜杠
    return "u_"+id;
  }


    /**
   * @function getResourceByReflect 获取图片名称获取图片的资源id的方法
   * @param imageName 图片名
   * @return resid
   */
  public int getResourceByReflect(String imageName) {
    Class drawable = R.drawable.class;
    Field field = null;
    int r_id = 0;

    if (imageName == null) {
      return r_id;
    }

    try {
      field = drawable.getField(imageName);
      r_id = field.getInt(field.getName());
    } catch (Exception e) {
      r_id = 0;
      Log.d(TAG, "image【"+imageName + "】field no found!");
    }

    if (r_id == 0) {
      r_id = context.getResources().getIdentifier(imageName, "drawable", context.getPackageName());
      Log.d(TAG, "image【"+ imageName + "】 drawable found ! r_id = " + r_id);
    }

    if (r_id == 0) {
      r_id = context.getResources().getIdentifier(imageName, "mipmap", context.getPackageName());
      Log.d(TAG, "image【"+ imageName + "】 mipmap found! r_id = " + r_id);
    }
    if (r_id == 0) {
      Log.d(TAG, "image【" + imageName + "】field no found!");
    }
    return r_id;
  }

}
